package com.crud.web.controller;

import com.crud.domain.ResultVo;
import com.crud.entity.Dept;
import com.crud.service.DeptService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author hyh
 * @date 2023/06/29 18:36:32
 */
@RestController
@RequestMapping("dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public ResultVo findAllOfPage(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize, @Param("name") String name){
        PageInfo<Dept> pageInfo = deptService.listDept(pageNum,pageSize,name);
        return ResultVo.success(pageInfo);
    }

    @PostMapping
    public ResultVo save(@RequestBody Dept dept){
        return deptService.save(dept) > 0 ? ResultVo.success("添加成功") : ResultVo.failure("添加失败");
    }

    @PutMapping
    public ResultVo update(@RequestBody Dept dept) {
        return deptService.update(dept) > 0 ? ResultVo.success("修改成功") : ResultVo.failure("修改失败");
    }

    @DeleteMapping("{id}")
    public ResultVo remove(@PathVariable Integer id){
        return deptService.remove(id) > 0 ? ResultVo.success("删除成功") : ResultVo.failure("删除失败");
    }
}
